package com.lite.business.service.contract.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lite.business.convert.contract.ContractConvert;
import com.lite.business.entity.contract.Contract;
import com.lite.business.entity.contract.ContractQueryWrapper;
import com.lite.business.entity.middletab.MiddleTab;
import com.lite.business.exception.BusinessException;
import com.lite.business.mapper.contract.ContractMapper;
import com.lite.business.mapper.middletab.MiddleTabMapper;
import com.lite.business.service.contract.ContractService;
import com.lite.business.service.middletab.MiddleTabService;
import com.lite.business.vo.contract.ContractVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author stranger
 * @since 2022-10-25
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractService {


    @Autowired
    ContractMapper contractMapper;

    @Autowired
    MiddleTabMapper middleTabMapper;

    @Autowired
    MiddleTabService middleTabService;
    @Autowired
    ContractConvert contractConvert;

    @Override
    @Cacheable(value = "getContractInfoById", key = "#contractId")
    public ContractVO getContractInfoById(Integer contractId) throws BusinessException {
        Contract contract = contractMapper.selectOne(new LambdaQueryWrapper<Contract>().eq(Contract::getId, contractId));

        if (Objects.isNull(contract)) {
            throw new BusinessException("该合同信息不存在");
        }

        //将其转换成合同视图层对象
        ContractVO contractVO = contractConvert.entityToVO(contract);

        //设置合同商品关联列表
        contractVO.setMiddleTabList(
                middleTabMapper.selectList(new LambdaQueryWrapper<MiddleTab>().eq(MiddleTab::getContractId, contract.getId())));

        return contractVO;
    }

    @Override
    @Cacheable(value = "listContractByPage")
    public List<Contract> listContractByPage(Integer page, Integer size, ContractQueryWrapper contractQueryWrapper) throws BusinessException {
        List<Contract> contractList = this.page(new Page<>(page, size), contractQueryWrapper.buildWrapper()).getRecords();
        if (Objects.isNull(contractList)) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "合同列表信息查询失败");
        }
        return contractList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(
            evict = {@CacheEvict(value = {"getContractInfoById"}, key = "#contractId"),
                    @CacheEvict(value = "listContractByPage", allEntries = true)
            }
    )
    public Boolean updateContractInfoById(Integer contractId, Contract contract) throws BusinessException {
        int count = contractMapper.updateById(contract);
        if (count <= 0) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "合同信息变更失败");
        }
        return true;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(
            evict = {@CacheEvict(value = {"getContractInfoById"}, key = "#contractId"),
                    @CacheEvict(value = "listContractByPage", allEntries = true)
            }
    )
    public Boolean removeContractInfoById(Integer contractId) throws BusinessException {
        int count = contractMapper.delete(new LambdaQueryWrapper<Contract>().eq(Contract::getId, contractId));
        if (count <= 0) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除合同信息失败");
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(
            evict = {@CacheEvict(value = "getContractInfoById", key = "#contractVO.id"),
                    @CacheEvict(value = "listContractByPage", allEntries = true)
            }
    )
    public Boolean addContractInfo(ContractVO contractVO) throws BusinessException {

        List<MiddleTab> middleTabList = contractVO.getMiddleTabList();

        Contract contract = contractConvert.voToEntity(contractVO);

        int concatCount = contractMapper.insert(contract);

        boolean isInserted = middleTabService.saveBatch(middleTabList);

        if (concatCount <= 0 || !isInserted) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "新增合同信息失败");
        }

        return true;
    }
}
